from nltk_contrib.readability.textanalyzer import *
import math

class ReadabilityTool:

    def __init__(self):
        self.tests = {}
        self.tests["ARI"] = self.ARI
        self.tests['Flesch Reading Ease'] = self.FleschReadingEase
        self.tests["Flesch-Kincaid Grade Level"] = self.FleschKincaidGradeLevel
        self.tests["Gunning Fog Index"] = self.GunningFogIndex
        self.tests["SMOG Index"] = self.SMOGIndex
        self.tests['Coleman Liau Index'] = self.ColemanLiauIndex
        self.text = ""
        self.analyzedVars = {}
        
    def __analyzeText(self, text):
      if text != self.text:
        t = textanalyzer('eng')
        t.analyzeText(text)
        words = t.getWords(text)
        charCount = t.getCharacterCount(words)
        wordCount = len(words)
        sentenceCount = len(t.getSentences(text))
        syllableCount = t.countSyllables(words)
        complexwordsCount = t.countComplexWords(text)
        averageWordsPerSentence = wordCount/sentenceCount
        
        analyzedVars = {}
        
        eps = 0.0000001
        analyzedVars['words'] = words
        analyzedVars['charCount'] = float(charCount) + eps
        analyzedVars['wordCount'] = float(wordCount) + eps
        analyzedVars['sentenceCount'] = float(sentenceCount) + eps
        analyzedVars['syllableCount'] = float(syllableCount) + eps
        analyzedVars['complexwordCount'] = float(complexwordsCount) + eps
        analyzedVars['averageWordsPerSentence'] = float(averageWordsPerSentence) + eps
        self.analyzedVars = analyzedVars

    def set_text(self, text):
      if text != self.text:
        self.__analyzeText(text)
        self.text = text
    
    def ARI(self, text):
        self.__analyzeText(text)
        score = 0.0
        analyzedVars = self.analyzedVars
        score = 4.71 * (analyzedVars['charCount'] / analyzedVars['wordCount']) + 0.5 * (analyzedVars['wordCount'] / analyzedVars['sentenceCount']) - 21.43
        return score
    
    def FleschReadingEase(self, text):
        self.__analyzeText(text)
        score = 0.0
        analyzedVars = self.analyzedVars        
        score = 206.835 - (1.015 * (analyzedVars['averageWordsPerSentence'])) - (84.6 * (analyzedVars['syllableCount']/ analyzedVars['wordCount']))
        return score
    
    def FleschKincaidGradeLevel(self, text):
        self.__analyzeText(text)
        score = 0.0
        analyzedVars = self.analyzedVars
        score = 0.39 * (analyzedVars['averageWordsPerSentence']) + 11.8 * (analyzedVars['syllableCount']/ analyzedVars['wordCount']) - 15.59
        return score
    
    def GunningFogIndex(self, text):
        self.__analyzeText(text)
        score = 0.0
        analyzedVars = self.analyzedVars
        score = 0.4 * ((analyzedVars['averageWordsPerSentence']) + (100 * (analyzedVars['complexwordCount']/analyzedVars['wordCount'])))
        return score
    
    def SMOGIndex(self, text):
        self.__analyzeText(text)
        score = 0.0
        analyzedVars = self.analyzedVars        
        score = (math.sqrt(analyzedVars['complexwordCount']*(30/analyzedVars['sentenceCount'])) + 3)
        return score
    
    def ColemanLiauIndex(self, text):
        self.__analyzeText(text)
        score = 0.0
        analyzedVars = self.analyzedVars        
        score = (5.89*(analyzedVars['charCount']/analyzedVars['wordCount']))-(30*(analyzedVars['sentenceCount']/analyzedVars['wordCount']))-15.8
        return score
